package com.budwk.app.sqfwDoc.controller;

import com.budwk.app.sqfwDoc.models.SqfwTemplateDict;
import com.budwk.app.sqfwDoc.service.SqfwTemplateDictService;
import com.budwk.app.sqfwDoc.vo.SqfwTemplateDictTreeVO;
import com.budwk.app.sqfwDoc.vo.SqfwTemplateDictSimpleVO;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.List;

/**
 * @author ZY.Wang
 * @since 2023/12/5
 */
@IocBean
@At("/platform/sqfw/sqfwTemplateDict")
@ApiDefinition(tag = "社区字典")
@Slf4j
public class SqfwTemplateDictController {

    @Inject
    private SqfwTemplateDictService sqfwTemplateDictService;

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询字典详情树")
    public Result<?> listDictTree() {
        List<SqfwTemplateDictTreeVO> list = sqfwTemplateDictService.listDictTree();
        return Result.data(list);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "根据parentId查询字典详情")
    public Result<?> listDictByParentId(@Param("parentId") String parentId) {
        List<SqfwTemplateDict> list = sqfwTemplateDictService.listDictByParentId(parentId);
        return Result.success(list);
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加字典详情")
    public Result<?> addDict(@Param("..") SqfwTemplateDict data) {
        String userId = SecurityUtil.getUserId();
        sqfwTemplateDictService.addDict(userId, data);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新字典详情")
    public Result<?> updateDict(@Param("..") SqfwTemplateDict data) {
        String userId = SecurityUtil.getUserId();
        sqfwTemplateDictService.updateDict(userId,data);
        return Result.success();
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取字典详情")
    public Result<?> getDetail(@Param("id") String id) {
        SqfwTemplateDictSimpleVO vo = sqfwTemplateDictService.getDetail(id);
        return Result.success(vo);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "删除字典详情")
    public Result<?> deleteDict(@Param("id") String id) {
        sqfwTemplateDictService.deleteDict(id);
        return Result.success();
    }
}
